package cn.tedu._03vehicle.base.interceptors;

import lombok.extern.slf4j.Slf4j;
import org.springframework.web.servlet.HandlerInterceptor;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.time.LocalTime;

@Slf4j
public class TimeAccessInterceptor implements HandlerInterceptor {
    @Override
    public boolean preHandle(HttpServletRequest request, HttpServletResponse response, Object handler) throws Exception {
        log.debug("1.preHandle方法执行");
        LocalTime now = LocalTime.now();
        int hour = now.getHour();
        if (hour < 12 || hour > 22) {

//            response.getWriter().write("<h1>当前时间不在开放时间范围内</h1>");
            throw new RuntimeException("当前时间不在开放时间范围内,请在6到22内访问");

        }
//        return HandlerInterceptor.super.preHandle(request, response, handler);
        return true;
    }
}
